#include "cameramodel.h"
#include <cstdio>
#include <cmath>

#include <QDebug>

using namespace std;

#define DEBUG 0
#include <GL/glut.h>
CameraModel::CameraModel(QWidget *parent) :
    QGLWidget(parent)
{
    target_x = 0;
    target_y = 0;
    target_z = 0;
    cur_x = 0;
    cur_y = 0;
    cur_z = 0;

}


void CameraModel::initializeGL(){
//    glShadeModel(GL_SMOOTH);
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClearDepth(1.0f);
    glLoadIdentity();
    glMatrixMode(GL_PROJECTION);
    glEnable(GL_BLEND);
    glEnable(GL_LINE_STIPPLE);
    glEnable(GL_LINE_SMOOTH);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//    glEnable(GL_DEPTH_TEST);
//    glDepthFunc(GL_LEQUAL);
//    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);


}

void CameraModel::resizeGL(int w, int h){

    glViewport(0, 0, (GLint)w, (GLint)h);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

}

static void drawCuboid(float x1, float y1, float z1, float x2, float y2, float z2){
    glBegin(GL_QUADS);
    glColor4f(0, 0, 1.0f, 1.0f);
    glVertex3f(x1, y1, z1);
    glVertex3f(x1, y2, z1);
    glVertex3f(x2, y2, z1);
    glVertex3f(x2, y1, z1);
    glColor4f(0, 1.0f, 0, 1.0f);
    glVertex3f(x1, y1, z2);
    glVertex3f(x1, y2, z2);
    glVertex3f(x2, y2, z2);
    glVertex3f(x2, y1, z2);
    glColor4f(1.0f, 0, 0, 1.0f);
    glVertex3f(x1, y1, z1);
    glVertex3f(x1, y2, z1);
    glVertex3f(x1, y2, z2);
    glVertex3f(x1, y1, z2);
    glColor4f(0, 0, 1.0f, 1.0f);
    glVertex3f(x2, y1, z1);
    glVertex3f(x2, y2, z1);
    glVertex3f(x2, y2, z2);
    glVertex3f(x2, y1, z2);
    glColor4f(0, 1.0f, 0, 1.0f);
    glVertex3f(x1, y1, z1);
    glVertex3f(x1, y1, z2);
    glVertex3f(x2, y1, z2);
    glVertex3f(x2, y1, z1);
    glColor4f(1.0f, 0, 0, 1.0f);
    glVertex3f(x1, y2, z1);
    glVertex3f(x1, y2, z2);
    glVertex3f(x2, y2, z2);
    glVertex3f(x2, y2, z1);

    glEnd();
}
void CameraModel::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glColor4f(0, 0, 1.0f, 1.0f);
    glColor4f(0, 0, 0, 0.5f);
    glBegin(GL_LINES);
    glLineWidth(1.0f);
    glLineStipple(1,0xFFFF);
    glVertex3f(0, 0, 1.0f);
    glVertex3f(0, 0, -1.0f);
    glVertex3f(0, 1.0f, 0);
    glVertex3f(0, -1.0f, 0);
    glVertex3f(1.0f, 0, 0);
    glVertex3f(-1.0f, 0, 0);
    glColor4f(1.0f, 0 , 0, 1.0f);
    glLineStipple(1, 0xF0F0);
    glLineWidth(10.0f);
    glVertex3f(0, 0, 0);
    #define PI 3.14159265
    glVertex3f(1.0f * cos(target_x * PI / 180), 1.0f * cos(target_y * PI / 180), 1.0f * cos(target_z * PI / 180));
    glColor4f(0, 1.0f , 0, 1.0f);
    glLineWidth(10.0f);
    glVertex3f(0, 0, 0);
    glVertex3f(1.0f * cos(cur_x * PI / 180), 1.0f * cos(cur_y * PI / 180), 1.0f * cos(cur_z * PI / 180));
    glEnd();
}

void CameraModel::changeCurrentDirection(int x, int y, int z){
    this->cur_x = x;
    this->cur_y = y;
    this->cur_z = z;
#if DEBUG
    qDebug()<<"ptz: x = "<<x<<" y = "<<y<<" z "<<z;
#endif
}

void CameraModel::changeTargetDirectionX(int x){
    this->target_x = x;
    updateGL();

}

void CameraModel::changeTargetDirectionY(int y){
    this->target_y = y;
    updateGL();

}

void CameraModel::changeTargetDirectionZ(int z){
    this->target_z = z;
    updateGL();

}

void CameraModel::reset(){
    this->target_x = 0;
    this->target_y = 0;
    this->target_z = 0;
    updateGL();
    emit targetDirectionChanged(target_x, target_y, target_z);
    emit resetDial(0);
}

void CameraModel::set(){
    emit targetDirectionChanged(target_x, target_y, target_z);
}
